// src/views/OwnerManagement.vue
<template>
  <div class="owner-management">
    <h1>车主管理</h1>
    <el-button @click="handleOpenAddDialog">添加车主</el-button>
    <el-table :data="owners" style="width: 100%">
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="contactNumber" label="联系电话"></el-table-column>
      <el-table-column prop="email" label="邮箱地址"></el-table-column>
      <el-table-column label="操作">
        <template v-slot:default="scope">
          <el-button @click="handleOpenEditDialog(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑车主信息对话框 -->
    <el-dialog title="车主信息" :visible.sync="dialogVisible">
      <el-form :model="form" :rules="rules" ref="ownerForm">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactNumber">
          <el-input v-model="form.contactNumber"></el-input>
        </el-form-item>
        <el-form-item label="邮箱地址" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveOwner">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      owners: [],
      dialogVisible: false,
      form: {
        id: null,
        name: '',
        contactNumber: '',
        email: ''
      },
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        contactNumber: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱地址', trigger: 'blur' }]
      }
    };
  },
  methods: {
    fetchOwners() {
      axios.get('/api/owners/')
        .then(response => {
          this.owners = response.data;
        })
        .catch(error => {
          console.error('Error fetching owners:', error);
        });
    },
    handleOpenAddDialog() {
      this.form = { id: null, name: '', contactNumber: '', email: '' };
      this.dialogVisible = true;
    },
    handleOpenEditDialog(owner) {
      this.form = { ...owner };
      this.dialogVisible = true;
    },
    handleDelete(id) {
      axios.delete(`/api/owners/${id}`)
        .then(() => {
          this.fetchOwners();  // 重新加载数据
          this.$message.success('车主删除成功');
        })
        .catch(error => {
          console.error('Error deleting owner:', error);
          this.$message.error('车主删除失败');
        });
    },
    saveOwner() {
      const method = this.form.id ? 'put' : 'post';
      const url = this.form.id ? `/api/owners/${this.form.id}` : '/api/owners/';
      axios[method](url, this.form)
        .then(() => {
          this.dialogVisible = false;
          this.fetchOwners();
          this.$message.success('车主信息保存成功');
        })
        .catch(error => {
          console.error('Error saving owner:', error);
          this.$message.error('车主信息保存失败');
        });
    }
  },
  created() {
    this.fetchOwners();
  }
}
</script>

<style scoped>
/* 样式可以根据需要添加 */
</style>
